using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sudoku.GamePlay;
using Sudoku;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Sudoku.Menu
{
	class CellButton : Button
	{
		private BoardCell boardCell;
		private Color fixedColor;
		private Color numberColor;

		private Texture2D selBg;
		private Color selColor;
		private Rectangle selRect;

		public CellButton(Game game, BoardCell boardCell, Rectangle rect)
			: base (game, rect)
		{
			this.boardCell = boardCell;
		}

		public override void Initialize()
		{
			base.Initialize();

			fixedColor = new Color(5, 77, 110);
			numberColor = new Color(255, 255, 255);

			selBg = textureService.Texture("WhiteFill");
			selColor = Color.Pink;
			selColor.A = 100;
			selRect = new Rectangle(rect.X + 1, rect.Y, rect.Width - 3, rect.Height - 3);
		}

		public override void Draw(GameTime gameTime)
		{
			
			if (state == ButtonState.DOWN)
			{
				spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied);
				spriteBatch.Draw(selBg, selRect, selColor);
				spriteBatch.End();
			}

			spriteBatch.Begin();

			if (boardCell.Fixed)
				spriteBatch.Draw(Texture, rect, new Rectangle(boardCell.Value * 50, 0, 50, 50), fixedColor);
			else
			{
				if (boardCell.Value == 0)
				{
					for (int y = 0; y < 9; ++y)
					{
						if (boardCell.Guess[y])
						{
							Rectangle r = new Rectangle(rect.X + (50 / 3) * (y % 3), rect.Y + (50 / 3) * (y / 3), 50 / 3, 50 / 3);
							spriteBatch.Draw(Texture, r, new Rectangle((y + 1) * 50, 0, 50, 50), numberColor);
						}
					}
				}
				else spriteBatch.Draw(Texture, rect, new Rectangle(boardCell.Value * 50, 0, 50, 50), numberColor);
			}

				
			
			spriteBatch.End();
		}

		public BoardCell BoardCell
		{
			get { return boardCell; }
		}

	}
}
